package gao.xiaolei.controller;

import java.io.IOException;
import java.util.List;

import gao.xiaolei.entity.LogEnity;
import gao.xiaolei.entity.StatusList;
import gao.xiaolei.jgit.Jgit;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import gao.xiaolei.myExcepition.MyExcepition;

import javax.annotation.Resource;

@RestController
public class JgitController {

	@Resource(name = "Jgit")
	private Jgit jgit;

	/**
	 * 在linux目录建立远程仓库
	 */
	@RequestMapping(value = "/createRemote", method = RequestMethod.POST)
	public void createRemote(
			@RequestParam(name = "remoteName", required = true) String remoteName) {
		try {
			jgit.createRemote(remoteName);
		} catch (IOException | MyExcepition e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 在本机目录建立本地远程仓库
	 */
	@RequestMapping(value = "/createLocalRemote", method = RequestMethod.POST)
	public int createLocalRemote(
			@RequestParam(name = "gitName", required = true) String gitName) {
		int flag = 0;
		try {
			flag = jgit.createLocalRemote(gitName);
		} catch (IllegalStateException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 用于创建Git文件目录,如果没有此目录会自动创建
	 */
	@RequestMapping(value = "/creatLocalGit", method = RequestMethod.POST)
	public int creatLocalGit(
			@RequestParam(name = "gitName", required = true) String gitName) {
		int flag = 0;
		try {
			flag = jgit.creatLocalGit(gitName);
		} catch (IllegalStateException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 用于Git-add文件到缓冲区
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "fileName", required = true) String fileName) {
		try {
			jgit.add(gitName, fileName);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 用于Git-rm -f将文件移出缓冲区和工作区,没有add直接rm会无效,有add但没有commit直接rm会git status看不到记录,
	 */
	@RequestMapping(value = "/rm", method = RequestMethod.POST)
	public void rm(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "fileName", required = true) String fileName) {
		try {
			jgit.rm(gitName, fileName);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 用于Git-commit文件到提交区
	 */
	@RequestMapping(value = "/commit", method = RequestMethod.POST)
	public String commit(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "message", required = true) String message) {
		String commitId=null;
		try {
			commitId= jgit.commit(gitName, message);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return commitId;
	}

	/**
	 * 查看git文件夹状态(相当于git status)
	 */
	@RequestMapping(value = "/getStatus", method = RequestMethod.GET)
	public StatusList getStatus(
			@RequestParam(name = "gitName", required = true) String gitName) {
		StatusList sl = null;
		try {
			sl = jgit.getStatus(gitName);
		} catch (NoWorkTreeException | IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return sl;
	}

	/**
	 * 查看git提交日志(相当于git log)
	 */
	@RequestMapping(value = "/getLog", method = RequestMethod.GET)
	public List<LogEnity> getLog(
			@RequestParam(name = "gitName", required = true) String gitName) {
		List<LogEnity> log = null;
		try {
			log = jgit.getLog(gitName);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return log;
	}

	/**
	 * 回退到上一个版本(相当于git reset --hard HEAD^)
	 */
	@RequestMapping(value = "/resetLastOne", method = RequestMethod.POST)
	public void reset(
			@RequestParam(name = "gitName", required = true) String gitName) {
		try {
			jgit.reset(gitName);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 回退到指定版本(相当于git reset --hard 版本号)
	 */
	@RequestMapping(value = "/reset", method = RequestMethod.POST)
	public void reset(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "version", required = true) String version) {
		try {
			jgit.reset(gitName, version);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 创建分支(相当于git branch <name> )
	 */
	@RequestMapping(value = "/createBranch", method = RequestMethod.POST)
	public void createBranch(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bname", required = true) String bname) {
		try {
			jgit.createBranch(gitName, bname);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 把某个分支合并到当前分支(相当于git merge <name>)
	 */
	@RequestMapping(value = "/mergeBranch", method = RequestMethod.POST)
	public void mergeBranch(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bname", required = true) String bname) {
		try {
			jgit.mergeBranch(gitName, bname);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 删除分支(相当于git branch -d<name> )
	 */
	@RequestMapping(value = "/deleteBranch", method = RequestMethod.POST)
	public void deleteBranch(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bname", required = true) String bname) {
		try {
			jgit.deleteBranch(gitName, bname);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 切换分支(相当于git checkout <name> 如果有文件没有commit就不会执行)
	 */
	@RequestMapping(value = "/checkOutBranch", method = RequestMethod.POST)
	public void checkOutBranch(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bname", required = true) String bname) {
		try {
			jgit.checkOutBranch(gitName, bname);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 把获取当前仓库的所有分支名(相当于git branch )
	 */
	@RequestMapping(value = "/showAllBranch", method = RequestMethod.GET)
	public List<String> showAllBranch(
			@RequestParam(name = "gitName", required = true) String gitName) {
		List<String> branchList = null;
		try {
			branchList = jgit.showAllBranch(gitName);
		} catch (IOException | GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return branchList;
	}

	/**
	 * 把远程仓库clone到指定目录(相当于git clone )默认显示mastet分支,其他分支要自己切换
	 */
	@RequestMapping(value = "/clone", method = RequestMethod.POST)
	public int clone(
			@RequestParam(name = "remoteName", required = true) String remoteName,
			@RequestParam(name = "gitName", required = true) String gitName) {
		int flag = 0;
		try {
			flag = jgit.clone(remoteName, gitName);
		} catch (GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 把本地远程仓库clone到本地指定目录(相当于git clone )默认显示mastet分支,其他分支要自己切换
	 */
	@RequestMapping(value = "/cloneLocalRemote", method = RequestMethod.POST)
	public int cloneLocalRemote(
			@RequestParam(name = "localRemotePath", required = true) String localRemotePath,
			@RequestParam(name = "gitName", required = true) String gitName) {
		int flag = 0;
		try {
			flag = jgit.cloneLocalRemote(localRemotePath, gitName);
		} catch (GitAPIException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 向远程仓库某个分支push((相当于git push)) 或者用于新建分支向远程仓库的推送
	 */
	@RequestMapping(value = "/push", method = RequestMethod.POST)
	public void push(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bName", required = true) String bName,
			@RequestParam(name = "remoteName", required = true) String remoteName) {
		try {
			jgit.push(gitName, bName, remoteName);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 向本地远程仓库某个分支push((相当于git push)) 或者用于新建分支向本地远程仓库的推送
	 */
	@RequestMapping(value = "/pushLocalRemote", method = RequestMethod.POST)
	public void pushLocalRemote(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bName", required = true) String bName,
			@RequestParam(name = "localRemotePath", required = true) String localRemotePath) {
		try {
			jgit.pushLocalRemote(gitName, bName, localRemotePath);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 从远程仓库拉取特定分支((相当于git pull))
	 */
	@RequestMapping(value = "/pull", method = RequestMethod.POST)
	public void pull(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bName", required = true) String bName,
			@RequestParam(name = "remoteName", required = true) String remoteName) {
		try {
			jgit.pull(gitName, bName, remoteName);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 从本地远程仓库拉取特定分支((相当于git pull))
	 */
	@RequestMapping(value = "/pullLocalRemote", method = RequestMethod.POST)
	public void pullLocalRemote(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "bName", required = true) String bName,
			@RequestParam(name = "localRemotePath", required = true) String localRemotePath) {
		try {
			jgit.pullLocalRemote(gitName, bName, localRemotePath);
		} catch (GitAPIException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * 判断本地是否有这个git仓库
	 */
	@RequestMapping(value = "/isHave", method = RequestMethod.GET)
	public boolean isHave(
			@RequestParam(name = "gitName", required = true) String gitName) {
		return jgit.isHave(gitName);
	}

	/**
	 * 获取某个git仓库下的某个commit下的某个文件内容
	 */
	@RequestMapping(value = "/readFileFromCommit", method = RequestMethod.GET)
	public byte[] readFileFromCommit(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "commitId", required = true) String commitId,
			@RequestParam(name = "path", required = true) String path) {
		byte[] fileByte = null;
		try {
			fileByte = jgit
					.readFileFromCommit(gitName, commitId, path);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return fileByte;
	}

	/**
	 * 获取某个git仓库下的所有commitId(由新到旧)
	 */
	@RequestMapping(value = "/listCommitId", method = RequestMethod.GET)
	public List<String> listCommitId(
			@RequestParam(name = "gitName", required = true) String gitName) {
		List<String> commitList = null;
		try {
			commitList = jgit.listCommitId(gitName);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return commitList;
	}

	/**
	 * 获取某个git仓库下的某次提交的某个文件夹下的文件列表
	 * 
	 */
	@RequestMapping(value = "/listFilesOfCommit", method = RequestMethod.GET)
	public List<String> listFilesOfCommit(
			@RequestParam(name = "gitName", required = true) String gitName,
			@RequestParam(name = "commitId", required = true) String commitId,
			@RequestParam(name = "path", required = false, defaultValue = "") String path) {
		List<String> fileList = null;
		try {
			fileList = jgit.listFilesOfCommit(gitName, commitId, path);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return fileList;
	}

}
